package com.servlet;

import com.dao.StudentDao;
import com.entity.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/admin/showAll")
public class ShowAll extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentDao studentDao = SpringUtil.getBean(StudentDao.class);
        List<Student> students = studentDao.showAll();

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(students);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.close();
    }
}
